package com.cndw.kungfu.event.model;

import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.scene.PendantManager;
import com.cndw.kungfu.model.sys.ChatMain;

public class PendantHandler implements Runnable {

	private static int[] startTimes = { 20 * 3600 + 1800, 22 * 3600 + 1800 };

	public static final int autoSceneId = 2;

	public static boolean open = false;
	/**
	 * 任务的状态
	 */
	private boolean state;

	private PendantHandler(boolean state) {
		this.state = state;
	}

	@Override
	public void run() {
		if (state) {
			Context.getTimerServer().scheduleSeconds(new PendantHandler(false), 1800);
		}
		open = state;
		if (open) {
			PendantManager.pendantTask(autoSceneId, true);
			ChatMain.systemMessage(LanguageLib.get("www.pendant.open"));
			LogUtil.getMain().debug("pendant is start  ...");
		} else {
			PendantManager.pendantTask(autoSceneId, false);
			ChatMain.systemMessage(LanguageLib.get("www.pendant.close"));
			LogUtil.getMain().debug("pendant is stop ...");
		}
	}

	public static boolean isOpen(int sceneId) {
		if (sceneId == autoSceneId) {
			return open;
		}
		return true;
	}

	public static void runBoss() {
		int mornignTime = TimeProcesser.getMorningTime();
		int currentTime = TimeProcesser.getUnixTime();
		int planTime = currentTime - mornignTime;
		for (int i = 0; i < startTimes.length; i++) {
			int delay = startTimes[i] - planTime;
			delay = delay < 0 ? delay + 86400 : delay;
			Context.getTimerServer().scheduleAtFixedRate(new PendantHandler(true), delay, 86400);
		}
		LogUtil.getMain().debug("pandant is start .... ");
	}

}
